<template>
  <div class="comment-reply">
    <!-- 回复评论的头部 -->
    <!-- 1. 头部根据请求的返回的数据进行判断 -->
    <van-nav-bar
      :title="
        comment.reply_count > 0 ? `${comment.reply_count}条回复` : '暂无回复'
      "
      fixed
    >
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>
    <!-- 当前评论项 -->
    <div class="scroll-wrap">
      <comment-item :comment="comment"></comment-item>
      <van-cell title="全部回复"></van-cell>
      <!-- 全部评论 和获取评论的接口是一样的 只不过传参的时候类型是c-->
    <article-comment
      ref="article-comment"
      :articleId="comment.com_id"
      type="c"
    ></article-comment>
    </div>
    <!-- 底部区域 发布评论 -->
    <div class="reply-bottom">
      <van-button
        class="write-btn"
        size="small"
        round
        @click="isPostShow = true"
        >写评论</van-button
      >
      <!-- 发布评论 同样用到弹出层 -->
      <van-popup v-model="isPostShow" position="bottom">
        <comment-post
          :targetId="comment.com_id"
          @success="success"
          :artId="$route.params.articleId"
        ></comment-post>
      </van-popup>
    </div>
  </div>
</template>

<script>
import CommentItem from './comment-item.vue'
// 回复列表和文章的评论列表几乎是一样的 把之间封装的评论列表拿来使用
import ArticleComment from './article-comment.vue'
import CommentPost from './comment-post.vue'
export default {
  name: 'CommentReply',
  props: {
    // 接收父组件传递过来的当前评论
    comment: {
      type: Object,
      default: () => ({})
    }
  },
  components: {
    CommentItem,
    ArticleComment,
    CommentPost
  },
  data () {
    return {
      isPostShow: false,
      list: [],
      commentClone: this.comment

    }
  },
  methods: {
    success (data) {
      // 1.更新回复的数量
      //   console.log(this.commentClone)
      this.commentClone.reply_count++
      // 2.关闭弹出层
      this.isPostShow = false
      // 3.将发布的内容放在列表的最上面
      //   console.log(data.data.new_obj)
      this.$refs['article-comment'].unshiftComment(data)
    }
  }
}
</script>

<style lang="less" scoped>
.scroll-wrap {
  position: fixed;
  top: 92px;
  left: 0;
  right: 0;
  bottom: 88px;
  overflow-y: auto;
}
.reply-bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-top: 1px solid #d8d8d8;
  .write-btn {
    width: 60%;
  }
}
</style>
